package com.bond.controller;

import com.bond.common.CommonResult;
import com.bond.common.page.TableDataInfo;
import com.bond.common.util.PageUtils;
import com.bond.entry.DeviceInfo;
import com.bond.service.SysService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class ApiDistributionControoller {

    @Autowired
    private SysService sysService;

//    url: "/api/distribution/list",
//    createUrl:  "/api/distribution/add",
//    updateUrl: "/api/distribution/edit/{id}",
//    removeUrl: "/api/distribution/remove",
// /api/distribution/checkExists


    /**
     * 分配扫码枪
     */
    @PostMapping("/api/distribution/list")
    public TableDataInfo distributionList(DeviceInfo deviceInfo) {
        PageUtils.startPage();
        List<DeviceInfo> list = sysService.selectDeviceInfoList(deviceInfo);
        return getDataTable(list);
    }

    @PostMapping("/api/distribution/checkExists")
    public boolean distributionAddCheckExists(DeviceInfo deviceInfo) {
        return sysService.distributionCheckExists(deviceInfo);
    }

    @PostMapping("/api/distribution/add")
    public CommonResult distributionAdd(DeviceInfo deviceInfo) {
        sysService.distribution(deviceInfo);
        return CommonResult.OK();
    }


    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

}
